import 'package:flutter/cupertino.dart';

//appbar,顶部标题页面
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../util/app_color.dart';
import '../util/colors.dart';

class TopAppBar extends AppBar {
  TopAppBar(BuildContext context, //上下文，必须
      String titleName, //页面标题，必须
          {
        bool isCenterTitle = true, //是否中间居中，默认中间居中，参数可选
        bool automaticallyImplyLeading=true ,
        final actions, //右边部分，可能存放图标，文字等，可能还有点击事件，参数可选
        final backIcon = const Icon(
          Icons.arrow_back_ios,
          color: AppColor.colorDefaultText,
        ), //左边返回箭头图标，默认是<，可自定义，，参数可选也可以是文字
        final String rightText, //右边文字，参数可选
        final bool isRightTextBtn = false, //右边文字是否是按钮
        final Widget rightWidget, //右边图标
        final rightCallback, //右边文字或者图标的点击函数，参数可选
        final leftCallBack, //覆盖左侧按钮监听 默认关闭当前页面
        final backColor = Colors.white, //背景颜色，默认白色
      }) : super(
    backgroundColor: backColor,
    elevation: 0,
    title: Text(
      titleName,
      style: TextStyle(
          fontSize: isCenterTitle ? 16 : 22,
          color: AppColor.colorDefaultText),
    ),
    leading:automaticallyImplyLeading?IconButton(
        icon: backIcon,
        onPressed: leftCallBack == null
            ? () => Navigator.of(context).pop()
            : leftCallBack):Text(""),
    centerTitle: isCenterTitle,
    actions: _rightWidget(
        rightText, rightWidget, rightCallback, isRightTextBtn),
  );
}

List<Widget> _rightWidget(String rightText, Widget rightWidget, rightCallback,
    isRightTextBtn) {
  if (rightText == null && rightWidget == null) {
    return null;
  }

  if (rightText!=null) {
    return List.generate(
        1,
            (index) =>
        isRightTextBtn
            ? _rightBtn(rightText, rightCallback)
            : _rightTextWidget(rightText, rightCallback));
  }
  if (rightWidget != null) {
    return List.generate(
        1,
            (index) =>
            InkWell(onTap: () {
              rightCallback();
            }, child: Padding(
              padding: EdgeInsets.only(right: 10.0),
              child: rightWidget,
            ),));
  }
}

_rightTextWidget(String rightText, rightCallback) {
  return Padding(
    padding: EdgeInsets.only(right: 10.0),
    child: Center(
      child: GestureDetector(
        onTap: () {
          rightCallback();
        },
        child: Text(rightText,
            style: TextStyle(fontSize: 15, color: AppColor.colorDefaultText)),
      ),
    ),
  );
}

_rightBtn(String rightText, Function rightCallback) {
  return InkWell(
    onTap: () {
      rightCallback();
    },
    child: Container(
      alignment: Alignment.center,
      margin: EdgeInsets.only(top: 15, bottom: 15, right: 15),
      height: ScreenUtil().setHeight(10),
      width: ScreenUtil().setWidth(60),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(6),
        color: Color.fromRGBO(0, 220, 169, 1),
      ),
      child: Text(
        rightText,
        style: TextStyle(color: Colors.white, fontSize: ScreenUtil().setSp(14)),
      ),
    ),
  );
}

_appBar() {
  return AppBar(
    automaticallyImplyLeading: false,
    actions: <Widget>[
      Container(
        margin: EdgeInsets.fromLTRB(0, 10, 15, 10),
        width: width(64),
        height: height(10),
        child: ElevatedButton(
          style: ButtonStyle(
            padding: MaterialStateProperty.all(EdgeInsets.zero),
            backgroundColor: MaterialStateProperty.all(AppColor.colorTheme),
          ),
          onPressed: () {
            // _sendDynamic();
          },
          child: Text(
            "发布",
            style: TextStyle(color: Colors.white, fontSize: 14),
          ),
        ),
      )
    ],
    title: InkWell(
      onTap: () {
        // Navigator.of(context).pop();
      },
      child: Text(
        '取消',
        style: TextStyle(color: AppColor.colorDefaultText, fontSize: 16),
      ),
    ),
    elevation: 0,
    backgroundColor: Colors.white,
  );
}
